//
//  ChooseRoomTableViewCell.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/2.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "ChooseRoomTableViewCell.h"

#import "ACMacros.h"
#import "AppDrawer.h"
#import <TSMessageView.h>

@implementation ChooseRoomTableViewCell {
    NSArray *buttons;
    NSInteger lastRent;
    
    NSInteger averagePrice;
}

- (void)awakeFromNib {
    // Initialization code
    buttons = @[self.aRoomChooseButton, self.bRoomChooseButton, self.cRoomChooseButton];
    for (UIButton *button in buttons) {
        [button setHidden:true];
        [button.layer setBorderWidth:1];
        if (button.selected) {
            [button setBackgroundColor:RGBACOLOR(255, 118, 108, 1)];
            [button.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
        } else {
            [button setBackgroundColor:[UIColor clearColor]];
            [button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        }
        [button addTarget:self action:@selector(roomChooseButtomClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.firstLineView setHidden:true];
    [self.secondLineView setHidden:true];
    [self.firstViewHeight setConstant:0];
    [self.secondViewHeight setConstant:0];
    [self.minePriceLabel setText:[NSString stringWithFormat:@"0"]];
    [self.firstPriceTextfield setDelegate:self];
    [self.secondPriceTextfield setDelegate:self];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - Triggering Methods
- (void)roomChooseButtomClicked:(UIButton *)sender {
    averagePrice = self.wholeRent / self.houseCount;
    averagePrice = ceil((averagePrice / 100)) * 100;
    NSAttributedString *placeHolder = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"建议价格%ld",(long)averagePrice] attributes:@{NSForegroundColorAttributeName:RGBCOLOR(140, 140, 140), NSForegroundColorAttributeName:[UIFont boldSystemFontOfSize:14]}];
    [self.firstPriceTextfield setAttributedPlaceholder:placeHolder];
    [self.secondPriceTextfield setAttributedPlaceholder:placeHolder];
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setBackgroundColor:RGBACOLOR(255, 118, 108, 1)];
        [sender.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
    } else {
        [sender setBackgroundColor:[UIColor clearColor]];
        [sender.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    }
    NSInteger remainCount = buttons.count;
    NSInteger selectedCount = 0;
    for (UIButton *button in buttons) {
        if (button.selected) {
            selectedCount += 1;
        }
        if (button.hidden) {
            remainCount -= 1;
        }
    }
    remainCount -= selectedCount;
    if (selectedCount == 0) {
        self.firstLineRent = 0;
        self.secondLineRent = 0;
        [self.firstViewHeight setConstant:0];
        [self.firstLineView setHidden:true];
        [self.secondViewHeight setConstant:0];
        [self.secondLineView setHidden:true];
    } else if (remainCount == 2 && selectedCount != 0) {
        [self.firstViewHeight setConstant:45];
        [self.firstLineView setHidden:false];
        [self.secondViewHeight setConstant:45];
        [self.secondLineView setHidden:false];
    } else if (remainCount == 1) {
        self.secondLineRent = 0;
        [self.firstViewHeight setConstant:45];
        [self.firstLineView setHidden:false];
        [self.secondViewHeight setConstant:0];
        [self.secondLineView setHidden:true];
    } else {
        self.firstLineRent = 0;
        self.secondLineRent = 0;
        [self.firstLineView setHidden:true];
        [self.secondLineView setHidden:true];
        [self.firstViewHeight setConstant:0];
        [self.secondViewHeight setConstant:0];
    }
    [self configureLabelsWithSelectedNumber:selectedCount];
}

- (void)configureLabelsWithSelectedNumber:(NSInteger)selectedNum {
    switch (selectedNum) {
        case 0:
        {
            lastRent = 0;
        }
            break;
        case 1:
        {
            if (selectedNum == self.houseCount) {
                lastRent = self.wholeRent;
            } else {
                for (int i = 0; i < self.houseCount; i++) {
                    UIButton *button = buttons[i];
                    if (button.selected) {
                        switch (i) {
                            case 0:
                                [self.firstRoomLabel setText:@"RoomB"];
                                [self.secondRoomLabel setText:@"RoomC"];
                                break;
                            case 1:
                                [self.firstRoomLabel setText:@"RoomA"];
                                [self.secondRoomLabel setText:@"RoomC"];
                                break;
                            default:
                                [self.firstRoomLabel setText:@"RoomA"];
                                [self.secondRoomLabel setText:@"RoomB"];
                                break;
                        }
                    }
                }
                lastRent = self.wholeRent - self.firstLineRent - self.secondLineRent;
            }
        }
            break;
        case 2:
        {
            if (selectedNum == self.houseCount) {
                lastRent = self.wholeRent;
            } else {
                for (int i = 0; i < self.houseCount; i++) {
                    UIButton *button = buttons[i];
                    if (!button.selected) {
                        switch (i) {
                            case 0:
                                [self.firstRoomLabel setText:@"RoomA"];
                                break;
                            case 1:
                                [self.firstRoomLabel setText:@"RoomB"];
                                break;
                            default:
                                [self.firstRoomLabel setText:@"RoomC"];
                                break;
                        }
                    }
                }
                lastRent = self.wholeRent - self.firstLineRent;
            }
        }
            break;
        case 3:
        {
            lastRent = self.wholeRent;
        }
            break;
        default:
            break;
    }
    if (self.firstLineRent == 0) {
        [self.firstPriceTextfield setText:nil];
    } else {
        [self.firstPriceTextfield setText:[NSString stringWithFormat:@"%ld",(long)self.firstLineRent]];
    }
    if (self.secondLineRent == 0) {
        [self.secondPriceTextfield setText:nil];
    } else {
        [self.secondPriceTextfield setText:[NSString stringWithFormat:@"%ld",(long)self.secondLineRent]];
    }
    [self.minePriceLabel setText:[NSString stringWithFormat:@"%ld",(long)lastRent]];
}

- (void)textFieldValueChanged:(UITextField *)textField {
    if ([textField isEqual:self.firstPriceTextfield]) {
        self.firstLineRent = [self.firstPriceTextfield.text intValue];
    } else if ([textField isEqual:self.secondPriceTextfield]) {
        self.secondLineRent = [self.secondPriceTextfield.text intValue];
    }
    lastRent = self.wholeRent - self.firstLineRent - self.secondLineRent;
    if (lastRent < 0) {
        [TSMessage showNotificationInViewController:[UIApplication sharedApplication].keyWindow.rootViewController title:@"请输入正确的金额!" subtitle:nil type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
        [textField becomeFirstResponder];
    } else {
        [self.minePriceLabel setText:[NSString stringWithFormat:@"%ld",(long)lastRent]];
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *value;
    if ([string isEqualToString:@""]) {
        value = [textField.text substringToIndex:textField.text.length - 1];
    } else {
        if ([string isEqualToString:@"0"]) {
            if (textField.text.length == 0) {
                return false;
            }
        }
        value = [textField.text stringByAppendingString:string];
    }
    if ([textField isEqual:self.firstPriceTextfield]) {
        self.firstLineRent = [value integerValue];
    } else  if ([textField isEqual:self.secondPriceTextfield]) {
        self.secondLineRent = [value integerValue];
    }
    lastRent = self.wholeRent - self.firstLineRent - self.secondLineRent;
    if (lastRent < 0) {
        [self.delegate alertWithTitle:@"房间价格不能超过整租价格" andSubTitle:nil];
        return false;
    } else {
        return true;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return true;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if ([textField.text integerValue] < averagePrice - 300) {
        [self.delegate alertWithTitle:[NSString stringWithFormat:@"定价不能低于%ld",averagePrice - 300] andSubTitle:nil];
        return false;
    } else if ([textField.text integerValue] > averagePrice + 300) {
        [self.delegate alertWithTitle:[NSString stringWithFormat:@"定价不能高于%ld",averagePrice + 300]  andSubTitle:nil];
        return false;
    } else {
        return true;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.delegate textFieldEndEditing];
    [self.minePriceLabel setText:[NSString stringWithFormat:@"%ld",(long)lastRent]];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self.delegate textFieldBeginEditing];
}

@end
